/*
Replication Data for: Party campaign statements and portfolio allocation in coalition governments
version 1.0 (October 27, 2022)

Please cite the related publication:
Däubler, Thomas, Marc Debus and Alejandro Ecker (2022). "Party campaign statements and portfolio allocation in coalition governments." West European Politics. https://doi.org/10.1080/01402382.2022.2140397

file: data_analysis.do
*/


**# 0. initialize 
**# 0.1 default settings
clear all
set more off
set scheme sj
capture log close

**# 0.2 define and set directories
global WDIR      YOUR MAIN DIRECTORY REPLICATION FILES
global DATADIR   "$WDIR/data"
global TMPDIR 	 "$WDIR/tmp"
global SCRIPTDIR "$WDIR/scripts"
global OUTDIR    "$WDIR/out"
cd "$WDIR"

**# 0.3 check sub-folder structure
mata
	if (direxists("$DATADIR") + direxists("$OUTDIR") + direxists("$SCRIPTDIRDIR") + direxists("$TMPDIR") < 4) {
		_error(111, "Please check and replicate the sub-folder structure described in the readme.txt file")
		exit() 
	}
	else printf("")
end

**# 0.4 check dependencies with community-contributed commands
foreach package in estout fitstat {
	capture which `package'
	if _rc == 111 display as error "Please install the community-contributed command/ado-file `package' to replicate all analyses"
}

**# 0.5 load data set
use "$DATADIR/portfolio_allocation.dta", clear


**# 1. main analysis

**# 1.1 conditional logit-regressions - Table 1
est clear

* models 1-3
global CONTROLS c.cabinet_seat_share##i.finance 
local IVs salience_cmp salience_wmp saliencepos_wmp
forvalues x = 1/3 {
	local IV = word("`IVs'", `x')
	clogit outcome $CONTROLS `IV', group(group_id) vce(robust)
	fitstat
	est store model_`x'
}

* models 4-5
global CONTROLS c.cabinet_seat_share##i.finance salience_cmp
local IVs salience_wmp saliencepos_wmp
forvalues x = 1/2 {
	local IV = word("`IVs'", `x')
	clogit outcome $CONTROLS `IV', group(group_id) vce(robust)
	fitstat
	local n = `x' + 3
	est store model_`n'
}

**# 1.2 export model results
esttab model_* using "$OUTDIR/table_1.rtf" , b(2) se(2) ///
	star(+ 0.10 * 0.05 ** 0.01) pr2(2) obslast mtitles noomitted compress replace


**# 2. supplementary materials 

**# 2.1 conditional logit-regressions - Table A.3
* models 6-7
global CONTROLS c.cabinet_seat_share c.cabinet_seat_share#i.finance salience_cmp
local IVs salience_wmp saliencepos_wmp
forvalues x = 1/2 {
	local IV = word("`IVs'", `x')
	clogit outcome $CONTROLS `IV', group(country_cabinet_id) vce(robust)
	fitstat
	local n = `x' + 5
	est store model_`n'
}

**# 2.2 export model results
esttab model_6 model_7 using "$OUTDIR/table_A3.rtf" , b(2) se(2) ///
	star(+ 0.10 * 0.05 ** 0.01) pr2(2) obslast mtitles noomitted compress replace


**# 3. export results of model 5 for simulation of predicted probabilities in R
est restore model_5

preserve
clear
matrix B = e(b)', e(V)
svmat B
drop if B1 == 0 
foreach v of var B* {
	egen tmp = total(`v')
	if(tmp==0) {
		drop `v' 
	}
	drop tmp
}

save "$TMPDIR/results_model5.dta", replace
restore
